route.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import NextAuth from "next-auth";
  2. import CredentialsProvider from "next-auth/providers/credentials";
  3. import * as R from "ramda";
  4. const authOptions = {
  5. // https://generate-secret.vercel.app/32
  6. secret: process.env.SECRET,
  7. // Configure one or more authentication providers
  8. providers: [
  9. CredentialsProvider({
  10. // The name to display on the sign in form (e.g. 'Sign in with...')
  11. name: "Cocorobo cloud",
  12. // The credentials is used to generate a suitable form on the sign in page.
  13. // You can specify whatever fields you are expecting to be submitted.
  14. // e.g. domain, username, password, 2FA token, etc.
  15. // You can pass any HTML attribute to the <input> tag through the object.
  16. credentials: {
  17. userid: { label: 'userid', type: 'text' }
  18. // loginUsername: { label: "用户名", type: "text" },
  19. // loginPassword: { label: "密码", type: "password" },
  20. },
  21. async authorize(credentials, req) {
  22. return { userid: credentials.userid };
  23. // You need to provide your own logic here that takes the credentials
  24. // submitted and returns either a object representing a user or value
  25. // that is false/null if the credentials are invalid.
  26. // e.g. return { id: 1, name: 'J Smith', email: 'jsmith@example.com' }
  27. // You can also use the `req` object to obtain additional parameters
  28. // (i.e., the request IP address)
  29. /*
  30. const res = await fetch("https://beta.api.cocorobo.cn/api/user", {
  31. method: "POST",
  32. body: JSON.stringify(
  33. R.pick(["loginUsername", "loginPassword"], credentials)
  34. ),
  35. headers: {
  36. "Content-Type": "application/json",
  37. Origin: "https://edu.cocorobo.cn",
  38. },
  39. });
  40. if (res.status !== 200) {
  41. return null;
  42. }
  43. const resJson = await res.json();
  44. const user = resJson?.[0]?.[0];
  45. // If no error and we have user data, return it
  46. if (res.ok && user && user.active) {
  47. return { ...user, id: user.userid, name: user.username };
  48. }
  49. */
  50. },
  51. }),
  52. ],
  53. callbacks: {
  54. async session({ session, user }) {
  55. console.log(session, user);
  56. // Send properties to the client, like an access_token from a provider.
  57. session.user.id = user.userid;
  58. try {
  59. const res = await fetch(
  60. `https://pbl.cocorobo.cn/api/pbl/selectUser?userid=${user.userid}`,
  61. {
  62. method: "GET",
  63. headers: {
  64. "Content-Type": "application/json",
  65. },
  66. }
  67. );
  68. const username = (await res.json())?.[0]?.[0]?.username;
  69. session.user.name = username;
  70. }
  71. catch (e) {
  72. return null;
  73. }
  74. return session;
  75. },
  76. },
  77. };
  78. const handler = NextAuth(authOptions);
  79. export { handler as GET, handler as POST };